<?php
/**
 * Created by PhpStorm.
 * User: longli
 * VX: isa1589518286
 * Date: 2021/08/23
 * Time: 23:53
 * @link http://www.lmterp.cn
 */

namespace app\common\model;

/**
 * 渠道报价单
 * Class ChannelQuotation
 * @package app\common\model
 */
class ChannelCountryQuotation extends BaseModel
{
    protected $pk = 'quotation_id';

    protected $autoWriteTimestamp = 'datetime';

    protected $insert = ['create_by', 'update_by'];

    protected $update = ['update_by'];

    /**
     * 渠道可走国家报价单
     * @return \think\model\relation\BelongsTo
     * @date 2021/08/24
     * @author longli
     */
    public function setting()
    {
        return $this->belongsTo(ChannelCountrySettings::class, 'setting_id', 'setting_id');
    }

    /**
     * 总价
     * @param float|int $weight 重量g
     * @return float|int
     * @date 2021/08/23
     * @author longli
     */
    public function getTotalPrice($weight)
    {
        $price = $this->price;
        $bType = $this->setting->channel->getData('billing_type');
        // 实重计费
        if($bType == Channel::BILLING_TYPE_ENTITY) $price = $this->price * $weight / 1000;
        // 计算折扣
        if($this->discount < 100) $price *= $this->discount / 100;
        // 计算续重
        if($bType == Channel::BILLING_TYPE_FIRST && $weight > $this->continue_weight)
            $price += $this->getContinuePrice($weight);
        // 计算总价
        $totalPrice = $price + $this->register_price + $this->operation_price + $this->fuel_price
            + $this->customs_price + $this->backend_price + $this->other_price + $this->tax_price;
        return round($totalPrice, 2);
    }

    /**
     * 计算续重的价格
     * @param float|int $weight 重量g
     * @return float|int
     * @date 2021/09/03
     * @author longli
     */
    public function getContinuePrice($weight)
    {
        $p = $weight > $this->continue_weight
            ? ceil(($weight - $this->continue_weight) / $this->continue_weight) * $this->continue_weight_price
            : $this->price;
        if($this->discount < 100) $p *= $this->discount / 100;
        return abs($p);
    }

    /**
     * 获取价格详情
     * @param float|int $weight 重量g
     * @return array
     * @date 2021/09/03
     * @author longli
     */
    public function getPirceInfo($weight)
    {
        $totalPrice = $this->getTotalPrice($weight);
        $field = ['continue_weight_price', 'register_price', 'operation_price', 'fuel_price', 'customs_price', 'backend_price', 'other_price', 'tax_price'];
        $result = \app\common\library\Tools::visibleArray($field, $this->toArray());
        $result['price'] = $this->price;
        $bType = $this->setting->channel->getData('billing_type');
        if($bType == Channel::BILLING_TYPE_ENTITY)
        {
            $result['price'] = $totalPrice - array_sum($result);
        }
        else if($bType == Channel::BILLING_TYPE_FIRST && $totalPrice != $this->price)
        {
            $result['continue_weight_price'] = $this->getContinuePrice($weight);
            $result['price'] = $totalPrice - array_sum($result) + $this->price;
        }
        $result = array_filter($result, function($item)
        {
            return $item > 0;
        });
        asort($result);
        return $result;
    }
}
